'''
[力扣题目链接](https://leetcode.cn/problems/minimum-size-subarray-sum/)
添加测试用例，认识到测试的重要性；
该问题:1.判断左指针移动情况，以及cur_sum更新时刻和方式
      2.当数组不存在时返回的情况，采用何种形式:float('inf)
      3.如何更新min_len 
'''
from typing import List
class Solution:
    def minSubArrayLen(self, target: int, nums: List[int]) -> int:
        l,r,size = 0,0,len(nums)
        cur_sum,subl,min_len = 0,0,float('inf')
        while r < size:
            cur_sum += nums[r]
            while cur_sum >= target:
                subl = r - l + 1
                min_len = min(min_len,subl)
                cur_sum -= nums[l]
                l+=1
            r+=1
        return min_len if min_len != float('inf') else 0

if __name__ == '__main__':
    # target = 7
    # nums = [2,3,1,2,4,3]
    target = 15
    nums = [1,2,3,4,5]
    print(Solution().minSubArrayLen(target, nums))

    # Output: 2
    # Explanation: The subarray [4,3] has the minimal length under the problem constraint.

